//
//  HexEditor.m
//  BLE4.0Example
//
//  Created by zxy on 2017/10/10.
//  Copyright © 2017年 saint. All rights reserved.
//

#import "HexEditor.h"
#import <Masonry/Masonry.h>
#import <NSData+FastHex.h>
#import <YYKit.h>
@implementation HexEditor


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.scorllView = [[UIScrollView alloc]init];
        [self addSubview:self.scorllView];
        [self.scorllView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        
        self.address = [[UILabel alloc]init];
        [self.scorllView addSubview:self.address];
        [self.address mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scorllView);
            make.left.equalTo(self.scorllView);
            make.width.equalTo(@60);
            
        }];
        self.address.font = [UIFont fontWithName:@"Menlo-Regular" size:14];
        [self.address setPreferredMaxLayoutWidth:60];
        self.address.numberOfLines = 0;
        
        self.hexContent = [[UITextView alloc]init];
        [self.scorllView addSubview:self.hexContent];
        [self.hexContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scorllView);
            make.left.equalTo(self.address.mas_right);
            make.width.equalTo(@350);
            make.height.equalTo(@0);
            
        }];
        self.hexContent.showsVerticalScrollIndicator = NO;
        
        self.hexContent.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        
        self.msgContent = [[UILabel alloc]init];
        [self.scorllView addSubview:self.msgContent];
        [self.msgContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scorllView);
            make.left.equalTo(self.hexContent.mas_right);
            make.width.equalTo(@145);
            
        }];
        
        
        
        self.msgContent.font = [UIFont fontWithName:@"Menlo-Regular" size:14];
        [self.msgContent setPreferredMaxLayoutWidth:145];
        self.msgContent.numberOfLines = 0;
    }
    return self;
}


- (void)showData:(NSData *)data{
    if (!data) {
        return;
    }
    
    self.hexContent.bounces = NO;
    NSString *dataString = [data hexStringRepresentationUppercase:YES];
    self.hexContent.attributedText = [self seperarWith:dataString];
    self.hexContent.font = [UIFont fontWithName:@"Menlo-Regular" size:14];
    [self.hexContent layoutIfNeeded];
    [self.hexContent setNeedsLayout];
    
    
    [self.scorllView layoutIfNeeded];
    [self.scorllView setNeedsLayout];
    self.scorllView.contentSize = CGSizeMake(self.scorllView.frame.size.width, self.hexContent.contentSize.height);
    
    [self.hexContent mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scorllView);
        make.left.equalTo(self.address.mas_right);
        make.width.equalTo(@350);
        make.height.equalTo(@(self.hexContent.contentSize.height));
        
    }];
    
  
    
    //1.获取地址信息
    [self setAddressString:dataString];
    
    [self setAsscilString:dataString];
}

- (void)setAsscilString:(NSString *)dataString{
    
    NSMutableString *all = [[NSMutableString alloc]init];
  
    
    
    for (int i = 0; i < dataString.length; i+=2) {
        
        if (i !=0 && i % 32 == 0) {
            [all appendString:@"\n"];
        }
        NSString *code = [dataString substringWithRange:NSMakeRange(i, 2)];
        NSData *data = [NSData dataWithHexString:code];
        NSString *ascString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        if (ascString) {
            [all appendString:ascString];
        }else{
            [all appendString:@"*"];
        }
        
        
    }
    self.msgContent.text = all;
}

- (void)setAddressString:(NSString *)dataString{
    
    int mount = (int)(dataString.length % 32 );
    int count = (int)(dataString.length / 32 );
    
    if (mount > 0) {
        count+=1;
    }
    NSMutableString *address = [[NSMutableString alloc]init];
    for (int i = 0; i < count; i ++) {
        
        NSMutableString *temp = [[NSMutableString alloc]initWithString:[NSString stringWithFormat:@"%x",i*16]];
        
        int fixCount = (int)(6 - temp.length);
        for (int j = 0; j < fixCount; j ++) {
            [temp insertString:@"0" atIndex:0];
        }
        
        [address appendString:[NSString stringWithFormat:@"%@:\n",temp]];
    }
    
    
    self.address.text = address;
}


- (NSMutableAttributedString *)seperarWith:(NSString *)sourceStr{
    NSMutableString *data = [[NSMutableString alloc]initWithString:sourceStr];
    NSString *newStr = [data stringByReplacingOccurrencesOfString:@" " withString:@""];;
    NSString *temp = nil;
    NSMutableString *all = [[NSMutableString alloc]init];
    NSMutableAttributedString *allAttributedString = [[NSMutableAttributedString alloc]init];
    
    BOOL hightLight = YES;
    for(int i =0; i < [newStr length]; i++)
    {
        temp = [newStr substringWithRange:NSMakeRange(i, 1)];
        
        BOOL isChange = [self.changeIndex containsObject:[NSString stringWithFormat:@"%d",i]];
        
        
        
        
        if (((i+1)%4 == 0)) {
            NSAttributedString *attr = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@ ",temp] attributes:@{
                                                                                                                                      NSForegroundColorAttributeName: hightLight? [UIColor blueColor] : [UIColor blackColor],
                                                                                                                                    
                                                                                                                                      }];
            
            [allAttributedString appendAttributedString:attr];
            
            hightLight = !hightLight;
            [all appendFormat:@"%@ ",temp];
        }else{
            [all appendFormat:@"%@",temp];
            
            NSAttributedString *attr = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",temp] attributes:@{
                                                                                                                                      NSForegroundColorAttributeName: hightLight? [UIColor blueColor] : [UIColor blackColor],
                                                                                                                                      
                                                                                                                                      }];
            
            [allAttributedString appendAttributedString:attr];
          
        }
        
        if (isChange) {
            NSRange changeRange = NSMakeRange(i, 1);
            [allAttributedString addAttributes:@{
                                                NSForegroundColorAttributeName: [UIColor redColor],
                                                
                                                } range:changeRange];
        }
    }
    return allAttributedString;
}


- (void)setSourceData:(NSData *)sourceData{
    _sourceData = sourceData;
    [self showData:_sourceData];
    
}


- (void)updateShouceData:(NSString *)text{
    NSString *string = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSData *hexData = [NSData dataWithHexString:string];
    self.sourceData = hexData;
}

- (NSMutableArray *)changeIndex{
    if (!_changeIndex) {
        _changeIndex = [[NSMutableArray alloc]init];
    }
    return _changeIndex;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
